// pages/setPassword/setPassword.js
const db = wx.cloud.database();
const app = getApp();
import {
  hexMD5
} from "../../utils/md5.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    havePassword: false,
    password: {
      old: "",
      new: "",
      again: ""
    }
  },
  // ----------------------------------------------------

  inputChange(e) {
    this.setData({
      ['password.' + e.target.id]: e.detail.value
    })
  },
  confirm() {
    let that = this;
    if (this.data.password.old == "" && this.data.havePassword) {
      wx.showToast({
        title: '原密码为空',
        icon: "error"
      });
      return;
    }
    if (this.data.password.new == "") {
      wx.showToast({
        title: '新密码为空',
        icon: "error"
      });
      return;
    }
    let reg = /^(?=.*[0-9\!@#\$%\^&\*])(?=.*[a-zA-Z]).{8,16}$/;
    let result = reg.test(this.data.password.new);
    if (!result) {
      wx.showModal({
        content: "新密码不合法",
        icon: "error",
        showCancel: false
      })
      return;
    }
    if (this.data.password.new != this.data.password.again) {
      wx.showModal({
        content: "两次密码输入不一致",
        showCancel: false
      });
      return;
    }

    if (this.data.havePassword) {
      this.havePasswordConfirm();
    } else {
      this.noPasswordConfirm();
    }
  },
  noPasswordConfirm() {
    let that = this;
    db.collection('users').where({
      _openid: app.personInfo._openid
    }).update({
      data: {
        password: hexMD5(that.data.password.new)
      },
      success: res => {
        app.personInfo.password = hexMD5(that.data.password.new);
        wx.showToast({
          mask: true,
          title: '修改成功',
        });
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)

      },
      fail: res => {
        wx.showToast({
          title: '您的网络异常',
        })
        console.log(res);
      }
    })
  },
  havePasswordConfirm() {
    let that = this;
    //密码校验成功
    if (hexMD5(this.data.password.old) == app.personInfo.password) {
      if (that.data.password.old == that.data.password.new) {
        wx.showModal({
          content: "新旧密码一致，不需要修改",
          showCancel: false
        });
        return;
      } else {
        // 新旧密码不一致时
        db.collection('users').where({
          _openid: app.personInfo._openid
        }).update({
          data: {
            password: hexMD5(that.data.password.new)
          },
          success: res => {
            app.personInfo.password = hexMD5(that.data.password.new);
            wx.showToast({
              title: '修改成功',
              mask: true
            });
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          },
          fail: res => {
            console.log(res);
            wx.showToast({
              title: '您的网络异常',
              icon: "error"
            })
          }
        })
      }
    } else {
      wx.showToast({
        icon: "error",
        title: '原密码错误',
      })
    }

  },

  // --------------------------------------------------
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    db.collection('users').where({
      _openid: app.personInfo._openid
    }).get({
      success: res => {
        if (!res.data[0].password) {
          that.setData({
            havePassword: false
          })
        } else {
          that.setData({
            havePassword: true
          })
        }
      },
      fail: res => {

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})